package com.online.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.entity.ApiResult;
import com.online.entity.Teacher;
import com.online.service.TeacherService;
import com.online.util.ApiResultHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 老师
 */
@RestController
@Slf4j
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page, @PathVariable Integer size, @RequestParam(required = false) String keyword) {
        Page<Teacher> teacherPage = new Page<>(page, size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage, keyword);
        log.info("查询所有的教师");
        return ApiResultHandler.buildApiResult(200, "查询所有的教师", teacherIPage);
    }

   /* @GetMapping("/teachers/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page,@PathVariable Integer size){
        Page<Teacher> teacherPage = new Page<>(page,size);
        IPage<Teacher> teacherIPage = teacherService.findAll(teacherPage);
       log.info("查询所有的教师");
       return ApiResultHandler.buildApiResult(200,"查询所有的教师",teacherIPage);
    }*/

    //todo 前端页面没有搜索框
    @GetMapping("/teacher/{teacherId}")
    public  ApiResult findById(@PathVariable("teacherId") Integer teacherId){
        log.info("根据id查询教师");
        return ApiResultHandler.success(teacherService.findById(teacherId));
    }
    @DeleteMapping("/teacher/{teacherId}")
    public ApiResult deleteById(@PathVariable("teacherId") Integer teacherId){
        log.info("根据id删除教师成功");
        return  ApiResultHandler.success(teacherService.deleteById(teacherId));
    }

    @PutMapping("/teacher")
    public ApiResult update(@RequestBody Teacher teacher){
        log.info("修改教师个人信息成功");
        return ApiResultHandler.success(teacherService.update(teacher));
    }
    @PostMapping("/teacher")
    public ApiResult add(@RequestBody Teacher teacher){
        log.info("添加教师成功");
        return ApiResultHandler.success(teacherService.add(teacher));
    }
}
